﻿$.extend($.fn.datagrid.methods, {
    dgrid: function (jq, opts) {
        var options = opts;
        var headerUrl,
            listUrl,
            listQuery,
            headerQuery;
        if (options.headerUrl) {
            headerUrl = options.headerUrl;
        }
        if (options.listUrl) {
            listUrl = options.listUrl;

        }
        if (!listUrl) {
            console.log("请设置listUrl属性。");
            return;
        }
        if (options.params) {
            params = options.params;
        }
        if (!options.listQuery) {
            listQuery = options.headerQuery;
        } else {
            listQuery = options.listQuery;
        }
        if (options.headerQuery) {
            headerQuery = options.headerQuery;
        }

        var opts = null;
        try {
            opts = $(jq).datagrid('options');
        } catch (e) { };
        if (opts != null) { //解决刷新出现列表加载两次的问题
            opts.url = null;
        }

        $.ajax({
            //提交数据的类型 POST GET
            type: "POST",
            //提交的网址
            url: headerUrl,
            //提交的数据
            data: headerQuery,
            //返回数据的格式
            datatype: "json",
            //成功返回之后调用的函数             
            success: function (data) {
            	if(typeof(data) === "string"){
		        	data = JSON.parse(data);
		        }
                if (!data.result) {
                    $.messager.alert("提示信息", data.message);
                    return;
                }
               

                var colWidth = 0;
                var index21 = null;
                for (var j = 0; j < data.rows.length; j++) {

                    if (data.rows[j].title == "站点名称") {
                        data.rows[j].width = 150;
                        colWidth += 150;
                    } else {
                        data.rows[j].width = 100; //默认添加width属性，使表格宽度自适应
                        colWidth += 100;                       

                        data.rows[j].formatter = function (value, row, index) {
                            if (value != null) { //不为空,处理包含','的数据
                                if (value.toString().indexOf(',') != -1) {

                                    var objArr = value.toString().split(',');
                                    var curValue = objArr[0];
                                    var isExceed = "";
                                    if (objArr.length > 2) {
                                        var maxValue = objArr[1];
                                        isExceed = objArr[2];
                                        var minValue = objArr[3];
                                        var paramType = objArr[4];
                                        var paramUnit = objArr[5];

                                        if (paramType == '10') {
                                            return "<span style='display:block;'><img src='images/Status" + curValue + ".gif' style='width:22px;height:22px;'/></span>";
                                        } else {
                                            var isOver = "未知";
                                            var color = 'black';
                                            var title = "";
                                            if (minValue == "" || maxValue == "") {
                                                title = '监测值:' + curValue + ',标准值:无,未知';
                                                return "<span title='" + title + "' style='display:block;'><font style='color:black';>" + curValue + "</font></span>";
                                            }


                                            if (!(curValue == null || curValue == "" || curValue == "--")) {
                                                if (isExceed == "0") {
                                                    isOver = '未超标';
                                                } else {
                                                    isOver = '超标';
                                                    color = 'red';
                                                }
                                            }

                                            if (minValue == maxValue && minValue != "") {
                                                title = '监测值:' + curValue + ',标准值:' + maxValue + ',' + isOver;
                                            } else {
                                                title = '监测值:' + curValue + ',标准值:' + minValue + '-' + maxValue + ',' + isOver;
                                            }

                                            return "<span title='" + title + "' style='display:block;'><font style='color:" + color + "';>" + curValue + "</font></span>";
                                        }
                                    } else {
                                        isExceed = objArr[1];
                                        var color = 'black';
                                        if (isExceed == "1") {
                                            color = 'red';
                                        }

                                        return "<span style='display:block;'><font style='color:" + color + "';>" + curValue + "</font></span>";
                                    }

                                }
                                return value;
                            }
                        };
                    }
                }

                if (document.documentElement.clientWidth < colWidth) {
                    options.fitColumns = false;
                } else {
                    options.fitColumns = true;
                }
                var len = data.rows.length;
                for (var n = 0; n < len; n++) {
                    data.rows[n].align = 'center';
                }
                options.columns = [data.rows]; //把返回的列数据赋于datagrid的columns属性 
                $(jq).datagrid(options); //根据配置选项，生成datagrid 
                $(jq).datagrid({
                    url: listUrl,
                    queryParams: listQuery
                }); //载入数据 
            },
            //调用执行后调用的函数
            complete: function (XMLHttpRequest, textStatus) { },
            //调用出错执行的函数
            error: function () { }
        });
    }
});
